

function promiseAll(promises) {
    return new Promise((resolve, reject) => {
        if (!Array.isArray(promises)) {
            throw new TypeError('argument must be a array');
        }
        let count = 0;
        let len = promises.length;
        let res = [];
        for (let index = 0; index < len; index++) {
            Promise.resolve(promises[index])
                .then(value => {
                    console.log(index+1, value);
                    count++;
                    res[index] = value;
                    if (count == len) {
                        return resolve(res);
                    }
                }, error => {
                    return reject(error);
                })
        }
    })
}

// case1

// let p1 = new Promise((resolve, reject) => {
//     setTimeout(() => {
//         resolve(1)
//     }, 1000)
// })
// let p2 = new Promise((resolve, reject) => {
//     setTimeout(() => {
//         resolve(2)
//     }, 2000)
// })
// let p3 = new Promise((resolve, reject) => {
//     setTimeout(() => {
//         resolve(3)
//     }, 3000)
// })

// case2
var p1=Promise.resolve(1),
p2=Promise.resolve(2),
p3=Promise.resolve(3);

// Promise.all([p3, p1, p2]).then(res => {
//     console.log(values) // [ 3, 1, 2 ]
// })

promiseAll([p3, p1, p2]).then(values => {
    console.log(values) // [ 3, 1, 2 ]
})

// 2 1
// 3 2
// 1 3